import axios from 'axios';

// 配置 Axios 的默认请求地址（替换为你的后端服务地址）
const API_URL = 'http://192.168.0.107:8080/nepm.com'; // 例如：http://localhost:8080
const apiClient = axios.create({
  baseURL: API_URL,
  timeout: 10000, // 设置超时时间
  headers: {
    'Content-Type': 'application/json',
  },
});

// 封装通用的请求方法
const get = async (endpoint, params = {}) => {
  try {
    const response = await apiClient.get(endpoint, { params });
    return response.data;
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
};

const post = async (endpoint, data) => {
  try {
    const response = await apiClient.post(endpoint, data);
    return response.data;
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
};

const put = async (endpoint, data) => {
  try {
    const response = await apiClient.put(endpoint, data);
    return response.data;
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
};

const deleteRequest = async (endpoint) => {
  try {
    const response = await apiClient.delete(endpoint);
    return response.data;
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
};

// 导出通用方法
export { get, post, put, deleteRequest as delete };